<?php

class userController extends Controller
{

    public $initphp_list = array(
        'edit',
        'save',
        'del'
    );

    /**
     *
     * @var adminService
     */
    private $admin;


    public function __construct()
    {
        parent::__construct();
        $this->checkLogin();
        $this->admin = InitPHP::getService("admin");
    }

    public function run()
    {
        $data = $this->admin->getAll();
        $this->view->assign("menu_now", "user");
        $this->view->assign("data", $data);
        $this->view->set_tpl("manage/header", "F"); // 设置公共头部模板
        $this->view->set_tpl("manage/user/list");
        $this->view->set_tpl("manage/footer", 'L'); // 设置公共尾部模板
        $this->view->assign("title", "文章管理-凯巴斯-管理后台");
        $this->view->display();
    }

    public function edit()
    {
        $id = $this->controller->get_gp("id");
        if ($id > 0) {
            $data = $this->admin->getOne($id);
            $this->view->assign("id", $id);
            $this->view->assign("data", $data);
        }
        $this->view->assign("menu_now", "user");
        $this->view->set_tpl("manage/header", "F"); // 设置公共头部模板
        $this->view->set_tpl("manage/user/add");
        $this->view->set_tpl("manage/footer", 'L'); // 设置公共尾部模板
        $this->view->assign("title", "添加导航-凯巴斯-管理后台");
        $this->view->display();
    }

    public function del()
    {
        $id = $this->controller->get_gp("id");
        $this->admin->delete($id);
        JsMessage("删除成功！", "/manage/user/run");
    }

    public function save()
    {
        $id = $this->controller->get_gp("id");
        $data = $this->controller->get_gp(array(
            'nick_name',
            'admin_name',
            'email',
            'pass',
        ));
        if (!$data['pass']){
            unset($data['pass']);
        }else {
            $data['pass']=md5($data['pass']);
        }
        if ($id) {
            $this->admin->update($id, $data);
        } else {
            $data['add_time']=time();
            $this->admin->add($data);
        }
        JsMessage("保存成功！", '/manage/user/run');
    }


    /**
     * 检查登陆状态
     */
    public function checkLogin()
    {
        $session = $this->getUtil('session');
        $adminid = $session->get('adminid');
        if (empty($adminid)) {
            $function = $this->getLibrary('function');
            $function->goto_url('/manage/login');
            die();
        }
        $this->view->assign("adminname", $session->get('adminname'));
        $this->view->assign("adminid", $session->get('adminid'));
    }
}